package com.hundsun.yr.universal.p2p.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.Log;

/**
 * json数据处理类
 * 
 * @author tonglj
 * 
 */
public class JSONTool {
	private final static String TAG = "JSONTool";
	public final static String responseCode = "responseCode";
	public final static String OBJECT = "busiObject";
	public final static String errorMsg = "errorMsg";
	private HashMap<String, Object> map_leval1;

	public JSONTool(String result) {
		try {
			map_leval1 = toHashMap(result);
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param key
	 * @return
	 */
	public String getStringValue(String key) {
		if(map_leval1 != null){
			try{
				return map_leval1.get(key).toString();
			}catch(Exception e){
				return "";
			}
		}return "";
	}

	/**
	 * 获取错误提示信息
	 * 
	 * @param key
	 * @return
	 */
	public String getErrorInfo() {
		return getStringValue(errorMsg);
	}

	/**
	 * 
	 * @param key
	 * @return
	 */
	public Object getValue(String key) {
		if (!key.contains("/"))
			return map_leval1.get(key);
		else {
			HashMap<String, Object> map_child = map_leval1;
			StringTokenizer token = new StringTokenizer(key, "/");
			StringBuffer childName = new StringBuffer();
			StringBuffer childContent = new StringBuffer();
			while (token.hasMoreTokens()) {
				childName.append(token.nextToken());
				try {
					if (map_child.get(childName.toString()) == null)
						return null;

					childContent.append(map_child.get(childName.toString()).toString());
					
					if (childContent.indexOf("{") == -1) {
						return childContent.toString();
					}
					map_child = toHashMap(childContent.toString());
					childContent.delete(0, childContent.length()); // 清空childContent
					childName.delete(0, childName.length()); // 清空childName
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}
			return map_child;
		}
	}

	/**
	 * 是否请求成功
	 * 
	 * @return
	 */
	public boolean isSuccess() {
		return TextUtils.equals(getStringValue("errorCode"), "0000");
	}

	// /**
	// *
	// * @return
	// * @throws JSONException
	// */
	// public boolean isModifySuccess() {
	// try {
	// return TextUtils.equals(getLeval2Value(responseType).toString(), "SUCCESS");
	// } catch (Exception e) {
	// return false;
	// }
	// }

	/**
	 * 获取busiObject下的值
	 * 
	 * @param key1
	 * @param key2
	 * @return
	 * @throws JSONException
	 */
	public String getLeval2Value(String key) {
//		if (isSuccess()) {
			try {
				HashMap<String, Object> map_leval2 = getMap(OBJECT);
				if (map_leval2 != null) {
					return map_leval2.get(key).toString();
				}
			} catch (Exception e) {
			}
//		}
		return "";
	}

	/**
	 * 获取json map对象
	 * 
	 * @param key
	 * @return
	 * @throws JSONException
	 */
	public HashMap<String, Object> getMap(String key) throws JSONException {
		String result = map_leval1.get(key).toString();
		if (!TextUtils.isEmpty(result)) {
			return toHashMap(result);
		}
		return null;
	}

	/**
	 * json转为HashMap json格式：{"name":"admin","retries":"3fff","testname" :"ddd","testretries":"fffffffff"}
	 *
	 * @throws JSONException
	 *             json转化异常
	 */
	public static HashMap<String, Object> toHashMap(String strdata) throws JSONException {
		HashMap<String, Object> data = new HashMap<String, Object>();
		// 将json字符串转换成jsonObject
		JSONObject jsonObject = new JSONObject(strdata);
		Iterator it = jsonObject.keys();
		// 遍历jsonObject数据，添加到Map对象
		while (it.hasNext()) {
			String key = String.valueOf(it.next());
			Object value = jsonObject.get(key);
			data.put(key, value);
		}
		return data;
	}

}
